#include "CCallFuncDelay.h"

CCallFuncDelay* CCallFuncDelay::actionWithDuration(float duration, SEL_CallFunc selector)
{
	return CCallFuncDelay::create(duration, selector);
}

CCallFuncDelay* CCallFuncDelay::actionWithDuration(float duration, SEL_CallFuncO selectorO, CCObject* pObj)
{
	return CCallFuncDelay::create(duration, selectorO, pObj);
}

CCallFuncDelay* CCallFuncDelay::create(float duration, SEL_CallFunc selector)
{
	CCallFuncDelay *pCallFuncInterval = new CCallFuncDelay();
	pCallFuncInterval->initWithDuration(duration, selector);
	pCallFuncInterval->autorelease();

	return pCallFuncInterval;
}

bool CCallFuncDelay::initWithDuration(float duration, SEL_CallFunc selector)
{
	if (CCActionInterval::initWithDuration(duration))
	{
		m_eCallFuncDelayType = CALL_FUNC_DELAY_WITH_NULL;
		m_selector = selector;
		return true;
	}

	return false;
}

CCallFuncDelay* CCallFuncDelay::create(float duration, SEL_CallFuncO selectorO, CCObject* pObj)
{
	CCallFuncDelay *pCallFuncInterval = new CCallFuncDelay();
	pCallFuncInterval->initWithDuration(duration, selectorO, pObj);
	pCallFuncInterval->autorelease();

	return pCallFuncInterval;
}

bool CCallFuncDelay::initWithDuration(float duration, SEL_CallFuncO selectorO, CCObject* pObj)
{
	if (CCActionInterval::initWithDuration(duration))
	{
		m_eCallFuncDelayType = CALL_FUNC_DELAY_WITH_CCOBJECT;
		m_selectorO = selectorO;
		m_pObject = pObj;
		return true;
	}

	return false;
}
CCObject* CCallFuncDelay::copyWithZone(CCZone *pZone)
{
	CCZone* pNewZone = NULL;
	CCallFuncDelay* pCopy = NULL;
	if(pZone && pZone->m_pCopyObject) 
	{
		//in case of being called at sub class
		pCopy = (CCallFuncDelay*)(pZone->m_pCopyObject);
	}
	else
	{
		pCopy = new CCallFuncDelay();
		pZone = pNewZone = new CCZone(pCopy);
	}

	CCActionInterval::copyWithZone(pZone);

	if(getType()==CALL_FUNC_DELAY_WITH_CCOBJECT){
		pCopy->initWithDuration(m_fDuration, m_selectorO, m_pObject);
	}
	else{
		pCopy->initWithDuration(m_fDuration, m_selector);
	}

	CC_SAFE_DELETE(pNewZone);
	return pCopy;
}

void CCallFuncDelay::startWithTarget(CCNode *pTarget)
{
	CCActionInterval::startWithTarget(pTarget);
}

void CCallFuncDelay::update(float time)
{

	if (m_pTarget && time==1.0f)
	{
		if(getType()==CALL_FUNC_DELAY_WITH_CCOBJECT){
			(m_pTarget->*m_selectorO)(m_pObject);
		}
		else{
			(m_pTarget->*m_selector)();
		}
	}
}